/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.constants.ManufactureApplyConstants;
import com.xy.biz.rs.domain.req.*;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.rs.domain.resp.GetManufactureApplyResponse;
import com.xy.biz.rs.domain.query.ManufactureApplyQuery;
import com.xy.biz.rs.domain.converter.ManufactureApplyConverter;
import com.xy.biz.rs.client.ManufactureApplyClient;
import com.xy.biz.rs.service.ManufactureApplyService;

/**
 *
 *
 * @author wwj
 * @date 2021-4-14
 */
@Api(tags = "生产指令")
@Slf4j
@RestController
@RequestMapping("/api/rs/manufacture/apply")
public class ManufactureApplyController implements ManufactureApplyClient {

	@Autowired
	private ManufactureApplyService manufactureApplyService;

	@ApiOperation(value = "保存生产指令")
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveManufactureApply(@RequestBody SaveManufactureApplyRequest request) {
		return manufactureApplyService.saveManufactureApply(ManufactureApplyConverter.toBO(request));
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllManufactureApply(@RequestBody Collection<SaveManufactureApplyRequest> collection) {
		manufactureApplyService.saveAllManufactureApply(ManufactureApplyConverter.toBO(collection));
		return Result.success();
	}

	@ApiOperation(value = "更新生产指令")
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateManufactureApply(@RequestBody UpdateManufactureApplyRequest request) {
        return manufactureApplyService.updateManufactureApply(ManufactureApplyConverter.toBO(request));
	}

	@ApiOperation(value = "改变状态（下发、取消等）")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_status")
    public Result<?> updateManufactureApplyStatus(@RequestBody UpdateManufactureApplyStatusRequest request) {
        return manufactureApplyService.updateManufactureApplyStatus(ManufactureApplyConverter.toBO(request));
    }

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteManufactureApply(@RequestBody GetManufactureApplyRequest request) {
		manufactureApplyService.deleteManufactureApply(request.getManufactureApplyId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetManufactureApplyResponse> getManufactureApply(@RequestBody GetManufactureApplyRequest request) {
		return Result.success(ManufactureApplyConverter.toResponse(manufactureApplyService.getManufactureApply(request.getManufactureApplyId())));
	}


	@ApiOperation(value = "获取下发次序")
	@Validatable
	@PostMapping(value = "/get_next_sequence")
	public Result<Integer> getNextSequence(@RequestBody GetNextSequenceRequest request) {
		return Result.success(manufactureApplyService.getNextSequence(request));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetManufactureApplyResponse>> listManufactureApplyById(@RequestBody Collection<Long> collection) {
		return Result.success(ManufactureApplyConverter.toResponse(manufactureApplyService.listManufactureApplyById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetManufactureApplyResponse>> listManufactureApply(@RequestBody ListManufactureApplyRequest request) {
		return Result.success(ManufactureApplyConverter.toResponse(manufactureApplyService.listManufactureApply(ManufactureApplyConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countManufactureApply(@RequestBody ListManufactureApplyRequest request) {
		return Result.success(manufactureApplyService.countManufactureApply(ManufactureApplyConverter.toQuery(request)));
	}

	@ApiOperation(value = "分页查询")
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetManufactureApplyResponse>> pageManufactureApply(@RequestBody ListManufactureApplyRequest request) {
		ManufactureApplyQuery query = ManufactureApplyConverter.toQuery(request);
		List<GetManufactureApplyResponse> list = ManufactureApplyConverter.toResponse(manufactureApplyService.listManufactureApply(query));
		int count = manufactureApplyService.countManufactureApply(query);
		return Result.success(new Page<>(list, count));
	}

    @ApiOperation(value = "分页查询已下发指令（用于待办事项）")
    @PostMapping(value = "/page_released")
    public Result<Page<GetManufactureApplyResponse>> pageReleasedManufactureApply(@RequestBody ListManufactureApplyRequest request) {
        ManufactureApplyQuery query = ManufactureApplyConverter.toQuery(request);
        query.setStatus(ManufactureApplyConstants.STATUS_RELEASED);
		query.setReceiveUserId(UserContextHolder.getUserId());
        List<GetManufactureApplyResponse> list = ManufactureApplyConverter.toResponse(manufactureApplyService.listManufactureApply(query));
        int count = manufactureApplyService.countManufactureApply(query);
        return Result.success(new Page<>(list, count));
    }

}
